import {http,req} from '../httpUtil'
var jwt = require('jsonwebtoken');

export function userLogin(data){
    return req({
        method : 'post',
        url : 'mock/testLogin',
        data : data
    });
}

export function userLogins(data){
    return http({
        method : 'post',
        url : 'user/getUserByLogin',
        data : data
    });
}

export function updateUser(data){
    return http({
        method: "post",
        url: "user/updateUser",
        data: data
    })
}

export function addUser(data){
    return http({
        method: "post",
        url: "user/addUser",
        data: data
    })
}

export function deleteUser(uid){
    return http({
        method: "post",
        url: "user/deleteUser",
        data: uid
    })
}

/**
 * 获取当前用户信息
 * @param {*} token 
 */
export function getUserInfo(token) {
    var res = decodeToken(token);
    var data = {
        userName : res.sub,
        stationName : res.stationName
    }
    return http({
        method: 'post',
        url: 'user/getUserByParams',
        data: data
    }).catch((error) => {
        console.log(error);
    });
}

/**
 * 获取用户列表
 */
export function getUserInfoList(data){
    return http({
        method : 'post',
        url : 'user/getUserByParams',
        data : data
    }).catch((error) =>{
        console.log(error);
    });
}
/**
 * 移除token
 */
export function removeToken() {
    return new Promise(resolve => {
        sessionStorage.removeItem('access_token');
        resolve();
    })
}

/**
 * 解析token
 * @param {*} token 
 */
export function decodeToken(token) {
    var jwtDecode;
    try {
        jwtDecode = jwt.decode(token, { complete: true });
    } catch (error) {
        console.log(error)
    }
    var jwtdata = jwtDecode.payload;
    return jwtdata;
}